<extend name="public@base5"/>
<block name="main">
    <div class="wrap container">
    <form class="form margin-top-20">
    <foreach name="config" item="vo" key="key">
        <div class="row mb-3">
        <div class="input-group config_item col-2 md-2" id="{$key}" >
            <label class="form-control ">键名key:</label>
            <input type="text" class="form-control" name="key" value="{$key}">
            <label class="form-control col-2">键值value:</label>
            <input type="text" class="form-control" name="value" value="{$vo}">
            <a class="btn btn-danger" onClick="mdelete('{$key}')">
                    <i class="fa fa-close"></i> 删除
            </a>
        </div>
        </div>
    </foreach>
        <div class="row mb-3">
    <div class="input-group config_item">
        <label class="form-control">新增key</label>
        <input type="text" class="form-control" name="key" placeholder="键名" required="" aria-required="true" aria-invalid="false">
        <label class="form-control">新增value</label>
        <input type="text" class="form-control" name="value" placeholder="键值" required="" aria-required="true" aria-invalid="false">
    </div>
        </div>
        <div class="row mb-3">
    <a class="btn btn-primary" onClick="save()">保存</a>
        </div>
    </form>
    </div>
    <script>
        function mdelete(key){
            var div=$('#'+key);
            if(div && div.length>0){
                div.remove();
            }
        }
        function save(){
            var map = {};
            var list = $('.config_item');
            list.each(function(i,it){
               var inputs = $(it).find('input'); 
               var key = $(inputs[0]).val();
               var value = $(inputs[1]).val();
               if(key &&value){
                   map[key] = value;
               }
            });
            $.post('save',map,function(data){
                reloadPage(window);
            });
        }
    </script>
</block>
